<?php


class Sherpa_Model_Coupon extends WSB_Model
{

	/**
	 * @var array
	 */
	protected $_data = array(
		// sherpa_coupons
		'id' => null,
		'code' => null,
		'value' => null,
		'percent' => null,
		'cashed' => null,
		'minOrderValue' => null,
		'multipleUses' => null,
		'validFrom' => null,
		'validTo' => null,
		'modified' => null,
		'created' => null
	);

	/**
	 * @param float $total
	 * @return float
	 */
	public function getValueRemaining($total = 0) {
		if ($this->getPercent() == true) {
			if (($this->getMultipleUses() == false) && ($this->getCashed() != 0)) {
				return 0;
			}
			return $total / 100 * $this->getValue();
		}
		return $this->getValue() - $this->getCashed();
	}

	/**
	 * @param float $cashed
	 * @return float
	 */
	public function getNewValue($cashed) {
		if ($this->getPercent() == true && $this->getMultipleUses() == false) {
			return 0;
		}
		$newCouponValue = $this->getValueRemaining() - $cashed;
		if ($newCouponValue < 0) {
			$newCouponValue = 0;
		}
		return $newCouponValue;
	}

	/**
	 * @param float $orderTotal
	 * @param integer $basketId
	 * @return float Cashed value
	 */
	public function cashIn($orderTotal, $basketId)
	{
		$valueRemaining = $this->getValueRemaining($orderTotal);
		if ($valueRemaining > $orderTotal) {
			$cashed = $orderTotal;
		} else {
			$cashed = $valueRemaining;
		}
		$newValue = $this->getNewValue($cashed);

		if ($this->getMultipleUses() == false) {
			$data = array(
				'cashed' => $this->getCashed() + $cashed,
				'modified' => new Zend_Db_Expr('NOW()')
			);
			$id = intval($this->getId());
			if($id > 0) {
				$this->getGateway()->update('sherpa_coupons', $data, 'id=' . $this->getId());
				return $cashed;
			}
		}
		return 0;
	}

	/**
	 * @return boolean
	 */
	public function isEmpty()
	{
		return empty($this->_data['id']);
	}

	/**
	 * @param float $subtotal
	 * @return boolean
	 */
	public function isValid($subtotal)
	{
		require_once(WSB_MODULES_PATH . 'sherpa/validators/Coupon.php');
		$validator = new Sherpa_Validator_Coupon();
		return $validator->isValid($this->getCode(), array('subtotal' => $subtotal));
	}

	/**
	 * @return boolean
	 */
	public function getPercent()
	{
		return !empty($this->_data['percent']);
	}

	/**
	 * @return boolean
	 */
	public function getMultipleUses()
	{
		return !empty($this->_data['multipleUses']);
	}

	/**
	 * @param boolean $unix
	 * @return string|integer
	 */
	public function getValidFrom($unix = false) {
		$validFrom = $this->_data['validFrom'];
		return $unix ? strtotime($validFrom) : $validFrom;
	}

	/**
	 * @param boolean $unix
	 * @return string|integer
	 */
	public function getValidTo($unix = false) {
		$validTo = $this->_data['validTo'];
		return $unix ? strtotime($validTo) : $validTo;
	}

	/**
	 * @param boolean $forEmail
	 * @return string
	 */
	public function getFormattedMinOrderValue($forEmail = false) {
		$minOrderValue = $this->getMinOrderValue();
		return ($minOrderValue == (int) $minOrderValue) ? $minOrderValue . ($forEmail ? '.--' : '.&#150;') : sprintf('%01.2f', $minOrderValue);
	}

}
